return an interface or a class C#

101

return an interface or a class C# -

// The return of interface as an instance of that interface
// Ff the instance has a few instances then no need to worry about the type of instance
// This example returns an object that has implemented Ic
public interface Ic
{
    int Type { get; }
    string Name { get; }
}

public class A : Ic
{
}

public class B : Ic
{
}

public Ic func(bool flag)
{
     if (flag)
         return new A();
       return new B();

}

Comments

Submit
0 Comments